// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    address:{},
    cartList:[],
    allChecked:false,
    totalPrice:0,
    totalNum:0

  },
  
  
  onLoad: function (options) {
    
  },
  onShow: function () {
    const cartList = wx.getStorageSync("cart")||[];
    const address = wx.getStorageSync("address");
    // const allChecked = cartList.length? cartList.every(v=>v.checked):false;
    this.setData({address});
    this.setCart(cartList);
  },

  handleChkChange(e){
    const goods_id = e.currentTarget.dataset.id;
    let cartList = this.data.cartList;
    cartList.forEach(v => {
      if(v.goods_id===goods_id){
        
        v.checked =!v.checked
      }
    });
    // let index = cartList.findIndex(v=>v.goods_id===goods_id);
    // cartList[index].checked = !cartList[index].checked;
    this.setCart(cartList);
  },

  setCart(cartList){
    let allChecked=true;
    let totalPrice=0;
    let totalNum = 0;
    cartList.forEach(v => {
      if(v.checked){
        totalPrice+=v.num*v.goods_price;
        totalNum+=v.num
      }else{
        allChecked = false;
      }
    });
   allChecked = cartList.length!=0 ? allChecked:false;
    this.setData({
      cartList,
      allChecked,
      totalPrice,
      totalNum
    });
    wx.setStorageSync("cart", cartList);
      
  },

  handleChooseAddress(){
    wx.chooseAddress({
      success: (res) => {
        let address = res;
        address.all = res.provinceName+res.cityName+res.countyName+res.detailInfo;
        wx.setStorageSync("address", address);  
      }
    }); 
  },
  handleAllChecked(){
    let cartList = wx.getStorageSync("cart");
    let allChecked = !this.data.allChecked;
    cartList.forEach(v=>{
        v.checked= allChecked;
    });
    this.setCart(cartList);
  },
  handleNumChange(e){
    let cartList = wx.getStorageSync("cart");
    const goods_id = e.currentTarget.dataset.id;
    const status = e.currentTarget.dataset.status;
    
    let index = cartList.findIndex(v=>v.goods_id===goods_id);
    if(cartList[index].num===1 && status===-1){
        wx.showModal({
          title: '警告',
          content: '再减就要删除了',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (res) => {
            if (res.confirm) {
              cartList.splice(index,1);
              this.setCart(cartList);
            }else if(res.cancel){

            }
          },
        });
          
    }else{
      cartList[index].num+=status;
      this.setCart(cartList);
    }
  },
  handleSettle(){
    if(!this.data.address.userName){
        wx.showToast({
          title: '请先选择地址',
          icon: 'error',
          mask: true
        });  
    }else if(this.data.totalNum===0){
      wx.showToast({
        title: '未选择商品',
        icon: 'error',
        mask: true
      }); 
    }else{
      wx.navigateTo({
        url: '/pages/pay/index'
      });
        
    }
  }
})